/**数据字典对象*/
var dic = {
	//公共
	100:{'0':'否','1':'是'},
	101:{'0':'停用','1':'启用'},
	102:{'false':'否','true':'是'},
	103:{'false':'停用','true':'启用'},
	104:{'>=':'大于等于','>':'大于','<=':'小于等于','<':'小于','=':'等于','≠':'不等于','为空':'为空','匹配':'匹配','不匹配':'不匹配','无':'无'},
	105:{'p1':'p1','p2':'p2','p3':'p3','p4':'p4','p5':'p5'},
	//事中风控相关
	//产品类型
	100000:{'01':'陆鲸','02':'易货保','03':'易货嘀','04':'收单宝','05':'运宝','06':'总包','07':'班车',
			'08':'分包','09':'易票通','10':'传化支付','11':'嘟嘟电台','12':'会员中心','13':'保理','14':'园区通','15':'智慧物流','16':'旺载'},
	//策略类型 '1':'审核'
	100001:{'0':'放行','2':'阻断','3':'短信验证','4':'图片验证','5':'验证'},
	//风险类型
	100002:{'0':'设备异常','1':'IP异常','2':'账号异常','3':'找货异常','4':'垃圾注册','5':'异常登录','6':'账号盗用','7':'暴力破解','8':'异常撞库',
		    '9':'短信轰炸','10':'薅羊毛','11':'身份冒用','12':'虚假拨打'},
	//核查单状态
	100003:{'0':'待处理','1':'有风险','2':'无风险'},
	//风险等级
	100004:{'高':'高风险','中':'中风险','低':'低风险'},
	//风险业务类型
	100005:{'0':'配货券使用前'},
	//统计维度
	100006:{'PARTYID':'会员ID','CLIENTDFP':'设备指纹','IPADDRESS':'IP地址','PARTYNAME':'会员名','PASSWORD':'登录密码','MOBILENUMBER':'手机号码','CARPLATENUMBER':'车牌号码'},
	//场景
	100007:{'0':'陆鲸配货','1':'用户注册','2':'密码登录','3':'验证登录','4':'找回登录密码',
			'5':'修改登录密码','6':'修改手机号码','7':'陆鲸转盘抽奖','8':'陆鲸获取配货券','9':'陆鲸拨打电话积分','99':'通用'},
	//统计结果有效期
	100008:{'1mm':'1分钟','60mm':'60分钟','24hh':'24小时','31dd':'31天','12MM':'12个月','10YY':'10年','Never':'永久'},
	//脚本类型
	100009:{'java':'java'},
	//原始消息解析状态
	100010:{'0':'待解析','1':'解析成功','2':'解析失败'},
	//原始消息统计状态
	100011:{'0':'待统计','1':'统计成功','2':'统计异常','3':'统计失败'},
	//消息处理节点
	100012:{'0':'记录消息','1':'解析消息','2':'统计消息'},
	//消息处理日志级别
	100013:{'DEBUG':'DEBUG','INFO':'INFO','WARN':'WARN','ERROR':'ERROR'},
	//消息来源
	100014:{'KafkaOperateMsg':'会员操作日志','LjCallRecord':'陆鲸拨打电话',
			'LjPublishGoodsRecord':'陆鲸发货','LjSearchGoodsRecord':'陆鲸找货','LuJingMarketingLog':'陆鲸营销'},
	//业务类名
	100015:{'KafkaOperateMsg':'KafkaOperateMsg','LjCallRecord':'LjCallRecord','PartyOperateLog':'PartyOperateLog',
			'LjPublishGoodsRecord':'LjPublishGoodsRecord','LjSearchGoodsRecord':'LjSearchGoodsRecord','LuJingMarketingLog':'LuJingMarketingLog'},
	//redis键值模板
	100016:{'NULL':'NULL','timestamp':'timestamp','paryId':'paryId'},
	//会员操作类型logintype
	100017:{'LOGINTYPE_LOGIN_SMS':'短信登入','PASSWORD':'密码登入','PARTY_REG':'注册','LOGINTYPE_FORGET_PASSWD':'找回登录密码','PARTY_CHANGE_LOGIN_PWD':'修改登录密码','EXCHANGE_MOBILE':'修改手机号码'},
	100018:{'90##dd':'90天','180##dd':'半年','3##yy':'3年','Never##Never':'永久'}
}
/**初始化下拉选*/
function initSelect() {
	initControlByDic();
	initControlByURL();
	initControlBySysConfig();
	initControlByEnum();
	if($('select.chosen-select').trigger){
		$('select.chosen-select').trigger('chosen:updated');
	}
}
/**通过数据字典初始化下拉选
 * <select class="dic" dicId="字典ID">
 */
function initControlByDic() {
	var controls = $(".dic");
	if(!controls) {
		return;
	}
	for(var i = 0 ; i < controls.length; i ++) {
		var control = $(controls[i]);
		//取数据字典号
		var dicId = control.attr("dicId");
		var tagName = control.prop("tagName");
		
		if(!control || !dicId || !tagName) {
			continue;
		}
		//取字典值
		var json = dic[dicId];
		if(!json) {
			continue;
		}
		//未初始化
		if(!control.hasClass("inited")) {
			for (var key in json) {
				drawControl(control, tagName, key, json[key]);
		    }
			control.addClass("inited");
		}
	}
}
/**通过系统配置初始化控件
 * 控件中指定属性config即可，例如<select class="sysconfig" config="配置项名称">
 */
function initControlBySysConfig() {
	var controls = $(".sysconfig");
	if(!controls) {
		return;
	}
	
	for(var i = 0 ; i < controls.length; i ++) {
		var control = $(controls[i]);
		//取配置项
		var configName = control.attr("config");
		var tagName = control.prop("tagName");
		
		if(!control || !configName || !tagName) {
			continue;
		}
		//取字典值
		var json = getConfigJsonByType(configName);
		if(!json) {
			continue;
		}
		//未初始化
		if(!control.hasClass("inited")) {
			$.each(json, function(i, n) {
				drawControl(control, tagName, n["serialNumber"], n["typeName"]);
			});
			control.addClass("inited");
		}
	}
}

/**通过后端枚举初始化控件
 * 控件中指定属性enum即可，例如<select class="enum" enum="枚举类名">
 */
function initControlByEnum() {
	var controls = $(".enum");
	if(!controls) {
		return;
	}
	
	for(var i = 0 ; i < controls.length; i ++) {
		var control = $(controls[i]);
		//取配置项
		var enumName = control.attr("enum");
		var tagName = control.prop("tagName");
		
		if(!control || !enumName || !tagName) {
			continue;
		}
		//取字典值
		var json = getEnumInfoByName(enumName);
		if(!json) {
			continue;
		}
		//未初始化
		if(!control.hasClass("inited")) {
			for(var i = 0 ; i < json.length ; i ++){
				var item  = json[i];
				drawControl(control, tagName, item.value, item.message);
			}
			control.addClass("inited");
		}
	}
}

/**通过任意url初始化下拉选：20条以内的数据可以使用*/
function initControlByURL() {
	var controls = $(".url");
	if(!controls) {
		return;
	}
	for(var i = 0 ; i < controls.length; i ++) {
		var control = $(controls[i]);
		//下拉选
		if(control.prop("tagName") == 'SELECT' && !control.hasClass("inited")) {
			//取数据字典号
			var _url = control.attr("url");
			var valueField = control.attr("valueField");
			var textField = control.attr("textField");
			if(!_url || !_valueField || !_textField) {
				continue;
			}
			$.ajax({
					url : _url,
					type :'POST',
					dataType : "json",
					contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
					success:function(data) {
						var express = data.module || [];
						for(var i in express){
							control.append("<option value=" + express[i][valueField] + ">" + express[i][textField] + "</option>");
						}
						control.addClass("inited");
					}
			});
		}
	}
}

/**通过数据字典初始化下拉选*/
function initSelectByDicId(control, dicId) {
	if(!control || !dicId) {
		return;
	}
	var json = dic[dicId];
	if(!json) {
		return;
	}
	for (var key in json) {
		control.append("<option value=" + key + ">" + json[key] + "</option>");
    }
	control.addClass("inited");
}

/**渲染控件*/
function drawControl(control, tagName, _value, _text) {
	if(!control || !_value || !tagName) {
		return;
	}
	switch (tagName) {
		case 'SELECT':
			//下拉选
			control.append("<option value=" + _value + ">" + _text + "</option>");
			break;
		case 'DIV':
			//复选框
			control.append("<label><input type='checkbox' value='" + _value + "' class='vtype'/>&nbsp" + _text + "</label>&nbsp&nbsp");
			break;
		default:
			break;
	}
}